﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZBZH_Manage_Order.Models;
using ZBZH_Manage_Order.PublicMethod;
using Newtonsoft.Json.Linq;

namespace ZBZH_Manage_Order.Controllers.TakerMangement
{
    public class TakerController : Controller
    {
        //派送员管理控制器
        // GET: /Taker/

        //连接服务
        ServiceReference1.Service1Client wsdcser = new ServiceReference1.Service1Client();

        public ActionResult Index()
        {
            return View(); 
        }


        /// <summary>
        /// 获取系统的增删查改的权限
        /// </summary>
        /// <returns></returns>
        //public ActionResult GetPower()
        //{
        //    try
        //    {
        //        string[] PowerS = null;
        //        //string RetPower = "";
        //        Models.ControlPower RetPower = new Models.ControlPower();
        //        Model.View_OperUser user = (Model.View_OperUser)Session[SessionStorage.OperUser];
        //        if (user != null)
        //        {
        //            PowerS = user.RolePower.Split(',');//权限码
        //            //拼接增删查改的权限
        //            foreach (string pow in PowerS)
        //            {
        //                if (pow == "009011002")
        //                {
        //                    RetPower.Add = 1;
        //                }
        //                else if (pow == "009011003")
        //                {
        //                    RetPower.Dell = 1;
        //                }
        //                else if (pow == "009011004")
        //                { RetPower.Edit = 1; }
        //            }
        //        }
        //        return Json(RetPower);
        //    }
        //    catch (Exception ex)
        //    {
        //        return Json("操作异常：" + ex + "请联系管理员解决");
        //    }
        //}

        /// <summary>
        /// 根据调度员ID获取旗下派送员
        /// </summary>
        /// <param name="OperID"></param>
        /// <param name="jqgridparam"></param>
        /// <returns></returns>
        public ActionResult GetTaker(string OperID, JqGridParam jqgridparam)
        {
            try
            {
                Page<Model.Order_Taker_T> page = new Page<Model.Order_Taker_T>();//分页类定义
                List<Model.Order_Taker_T> list = new List<Model.Order_Taker_T>();
                Model.View_OperUser user = (Model.View_OperUser)Session[SessionStorage.OperUser];
                OperID = user.OperID.ToString();
                string result = wsdcser.GetOrder_Taker(OperID, 0, jqgridparam.page, jqgridparam.rows, GetYZM._YZM, 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
                if (bs == "1")//返回结果正常 
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    if (jieguo.Count > 0)
                    {
                        list = ParseClass<Model.Order_Taker_T>.JsonList(jieguo);//解析返回的结果\
                    }
                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = list
                    };
                    return Json(data);
                }
                else
                {
                    var data = new
                    {
                        total = "0",
                        rows = list
                    };
                    return Json(data);
                }
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }

        /// <summary>
        /// 增删改派送员信息
        /// </summary>
        /// <param name="addtaker"></param>
        /// <param name="tag"></param>
        /// <returns></returns>
        public ActionResult SetTaker(int Tag, Model.Order_Taker_T settaker)
        {
            try
            {
                Model.View_OperUser user = (Model.View_OperUser)Session[SessionStorage.OperUser];
                settaker.TakerState = 1;
                settaker.OperID = user.OperID;
                string json = settaker.ToJson();
                string result = wsdcser.SetOrder_Taker(json, Tag, "", 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();
                if (bs == "1")
                {
                    return Content(bs);
                }
                else
                {
                    return Content(searcho["Msg"].ToString());
                }
            }
            catch (Exception)
            {
                //return null;             
                 return RedirectToAction("LoginPage", "Login");  //czx:修改为登录者的Session异常时，直接跳转到登录页面
                
            }
        }
    }
}
